﻿using System.Collections.Generic;
using System.Linq;
using BearUtil.Hidden;
using Sirenix.OdinInspector;
using UnityEngine;
using XNode;
using Node = XNode.Node;

namespace BearUtil
{
    [Node.NodeWidthAttribute(300), CreateNodeMenu(menuName:"自定义节点/选项展示"), Title("选项")]
    public class ChoiceNode : BearNode
    {
        public override int type
        {
            get => (int) NodeType.CHOICE;
        }

        [FoldoutGroup("选项"), LabelText("文本 id"), ValueDropdown("GetLineKey", DropdownWidth = 200), OnValueChanged("switchContent")]
        public string textId;
        // 还没做完，没法显示内容
        [FoldoutGroup("选项"), TextArea, ReadOnly]
        public string content;
        
        [FoldoutGroup("选项"), HideLabel, ReadOnly]
        public List<string> choose = new List<string>();

        [Output(dynamicPortList = true), SerializeField] 
        public List<string> items = new List<string>();

        [FoldoutGroup("选项"), Button("增加")]
        public void Add()
        {
            if (choose == null)
                choose = new List<string>();
             
            choose.Add(textId);
            Update();
        }

        [FoldoutGroup("选项"), Button("减少")]
        public void Remove()
        {
            if (choose == null)
            {
                choose = new List<string>();
                return;
            }

            if (choose.Count <= 0)
                return;
            
            choose.RemoveAt(choose.Count - 1);
            Update();
        }

        public void Update()
        {
            if (choose == null || choose.Count <= 0)
                return;

            items = new List<string>();
            choose.ForEach( c => items.Add(c));
            
           var step =  Ports.GetEnumerator();
           while (step.MoveNext())
           {
               var port = (step.Current as NodePort);
           }
        }

        private List<string> GetLineKey()
        {
            var dialog = (graph as DialogGraph);
            if (dialog == null)
                return null;
        
            var n = (dialog.owner as DialogNode);
            if (n == null)
                return null;
            
            return n.property.GetLines();
        }
        
        private void switchContent(string lineKey)
        {
            var dialog = (graph as DialogGraph);
            if (dialog == null)
                return;
        
            var n = (dialog.owner as DialogNode);
            if (n == null)
                return;
            // switch content
            content = n.property.GetLineContent(lineKey);
        }
    }   
}
